
package com.platform.modules.mall.activity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.RestResponse;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.IAssociTagDataChange;
import com.platform.modules.mall.service.MallActivityService;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import com.platform.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 活动商品对应表Controller
 *
 * @author
 * @date 2021-08-11 16:21:47
 */
@RestController
@RequestMapping("mall/goodstoactivity")
public class MallGoodsToActivityController extends AbstractController {
    @Autowired
    private MallGoodsToActivityService mallGoodsToActivityService;
    @Resource
    private IAssociTagDataChange shopsActivityTag;
    @Resource
    private MallActivityService activityService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:goodstoactivity:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallGoodsToActivityEntity> list = mallGoodsToActivityService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询活动商品对应表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:goodstoactivity:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = mallGoodsToActivityService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:goodstoactivity:info")
    public RestResponse info(@PathVariable("id") String id) {
        MallGoodsToActivityEntity mallGoodsToActivity = mallGoodsToActivityService.getById(id);

        return RestResponse.success().put("goodstoactivity", mallGoodsToActivity);
    }

    /**
     * 新增活动商品对应表
     *
     * @param mallGoodsToActivity mallGoodsToActivity
     * @return RestResponse
     */
    @SysLog("新增活动商品对应表")
    @RequestMapping("/save")
    @RequiresPermissions("mall:goodstoactivity:save")
    public RestResponse save(@RequestBody MallGoodsToActivityEntity mallGoodsToActivity) {

        shopsActivityTag.associAdd(activityService.getById(mallGoodsToActivity.getActivityId()), mallGoodsToActivity.getGoodsId());

        mallGoodsToActivityService.add(mallGoodsToActivity);

        return RestResponse.success();
    }

    /**
     * 修改活动商品对应表
     *
     * @param mallGoodsToActivity mallGoodsToActivity
     * @return RestResponse
     */
    @SysLog("修改活动商品对应表")
    @RequestMapping("/update")
    @RequiresPermissions("mall:goodstoactivity:update")
    public RestResponse update(@RequestBody MallGoodsToActivityEntity mallGoodsToActivity) {
        MallGoodsToActivityEntity entity = mallGoodsToActivityService.getById(mallGoodsToActivity.getId());
        if (!entity.getGoodsId().equals(mallGoodsToActivity.getGoodsId())) {
            shopsActivityTag.associDelate(entity.getGoodsId());
            shopsActivityTag.associAdd(activityService.getById(mallGoodsToActivity.getActivityId()), mallGoodsToActivity.getGoodsId());
        }
        mallGoodsToActivityService.update(mallGoodsToActivity);
        return RestResponse.success();
    }

    /**
     * 根据主键删除活动商品对应表
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除活动商品对应表")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:goodstoactivity:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        List<String> list = mallGoodsToActivityService.listObjs(new LambdaQueryWrapper<MallGoodsToActivityEntity>()
                .in(MallGoodsToActivityEntity::getGoodsId, ids), Object::toString);
        shopsActivityTag.associDelate(list.stream().toArray(String[]::new));
        mallGoodsToActivityService.deleteBatch(ids);
        return RestResponse.success();
    }
}
